package com.cn.laoluobase.config;

import com.cn.laoluobase.jo.BusinessException;
import com.cn.laoluobase.jo.ResultsObj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger("GlobalExceptionHandler");

    /**
     * 处理Controller层通用 Exception 异常
     * 可以防止Controller未处理异常的程序中止，进行全局捕获
     * @ExceptionHandler(value = Exception.class) 可以定义多个。比如BusinessExceptionHandler,ServicesExceptionHandler
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResultsObj exceptionHandler(Exception e) {
        logger.error("全局异常：",e);
        return ResultsObj.fail(e.getMessage());
    }

    /**
     * 处理Controller层通用 BusinessException 异常
     * 可以防止Controller未处理异常的程序中止，进行全局捕获
     * @ExceptionHandler(value = Exception.class) 可以定义多个。
     */
    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public ResultsObj BizExceptionHandler(BusinessException e) {
        logger.error("全局业务异常：",e);
        return ResultsObj.fail(e.getMsg());
    }

}
